package com.xywh.wanandroid.compose_ui

import android.os.Bundle
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.material3.ProgressIndicatorDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.google.accompanist.systemuicontroller.SystemUiController
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.COLLECT_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.HOME_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.KNOWLEDGE_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.LOGIN_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.RANK_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.REGISTER_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.SCORE_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.SEARCH_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.SPLASH_ROUTE
import com.xywh.wanandroid.compose_ui.WanAndroidDestinations.WEB_VIEW_ROUTE
import com.xywh.wanandroid.compose_ui.collect.CollectScreen
import com.xywh.wanandroid.compose_ui.login.LoginScreen
import com.xywh.wanandroid.compose_ui.login.RegisterScreen
import com.xywh.wanandroid.compose_ui.main.MainScreen
import com.xywh.wanandroid.compose_ui.rank.RankScreen
import com.xywh.wanandroid.compose_ui.score.ScoreScreen
import com.xywh.wanandroid.compose_ui.search.SearchScreen
import com.xywh.wanandroid.compose_ui.splash.SplashScreen
import com.xywh.wanandroid.compose_ui.system.KnowledgeScreen
import com.xywh.wanandroid.compose_ui.webView.WebViewScreen
import com.xywh.wanandroid.data.model.bean.KnowledgeTreeItem
import com.xywh.wanandroid.event.EventViewModel
import com.xywh.wanandroid.ext.navigateBundle
import com.xywh.wanandroid.ext.navigateUpWan
import com.xywh.wanandroid.ext.toComposeWebView
import com.xywh.wanandroid.theme.Purple40
import com.xywh.wanandroid.util.CacheUtil
import com.xywh.wanandroid.viewmodel.MainComposeViewModel


@Composable
fun WanAndroidNavGraph(
    modifier: Modifier = Modifier,
    navController: NavHostController,
    startDestination: String = SPLASH_ROUTE,
    systemUiController: SystemUiController,
    useDarkIcons: Boolean
) {

    if (CacheUtil.isLogin()) {
        EventViewModel.isLogin.value = true
    }
    val mainComposeViewModel: MainComposeViewModel = hiltViewModel()

    NavHost(
        navController = navController,
        startDestination = startDestination,
        modifier = modifier
    ) {
        composable(route = SPLASH_ROUTE) {

            SplashScreen {

                navController.navigate(HOME_ROUTE)
            }

        }
        composable(route = LOGIN_ROUTE) {
            LoginScreen(
                navigateUp = {
                    navigateUpWan(navController)
                },
                navigateRegister = {
                    navController.navigate(REGISTER_ROUTE)
                },
                updateMainScreenCollectListData = {
                    mainComposeViewModel.updateCollectArticleListData()
                })
        }
        composable(route = REGISTER_ROUTE) {

            RegisterScreen {
                navigateUpWan(navController)
            }
        }
        composable(HOME_ROUTE) {
            SideEffect {
                systemUiController.setStatusBarColor(
                    color = Purple40,
                    darkIcons = useDarkIcons,
                )
            }
            MainScreen(
                mainComposeViewModel = mainComposeViewModel,
                navigateRank = {
                    navController.navigate(RANK_ROUTE)
                },
                navigateLogin = {
                    navController.navigate(LOGIN_ROUTE)
                },
                navigateScore = {
                    navController.navigate(SCORE_ROUTE)
                },
                navigateCollect = {
                    navController.navigate(COLLECT_ROUTE)
                },
                navigateSearch = {
                    navController.navigate(SEARCH_ROUTE)
                },
                navigateWebView = { title, link ->
                    toComposeWebView(navController, title, link)
                },
                navigateKnowledge = { title, itemData ->
                    navController.navigateBundle(
                        KNOWLEDGE_ROUTE,
                        Bundle().apply {
                            putString("title", title)
                            putParcelable("itemData", itemData)
                        }
                    )
                }
            )
        }
        composable(
            "${WEB_VIEW_ROUTE}?title={title}&&link={link}",
            arguments = listOf(
                navArgument("title") { defaultValue = "" },
                navArgument("link") { defaultValue = "" })
        ) { backStackEntry ->


            val title = backStackEntry.arguments?.getString("title")
            val link = backStackEntry.arguments?.getString("link")


            //进度条显示隐藏
            val transitionState = remember { MutableTransitionState(false) }.apply {
                targetState = true
            }
            //进度
            val progress = remember { mutableFloatStateOf(0f) }
            //进度条加载动画
            val animatedProgress by animateFloatAsState(
                targetValue = progress.value,
                animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec, label = ""
            )

            if (link != null && title != null) {
                WebViewScreen(
                    title,
                    link,
                    transitionState,
                    progress,
                    animatedProgress
                ) {
                    navigateUpWan(navController)
                }
            }

        }
        composable(KNOWLEDGE_ROUTE) { backStackEntry ->

            val topBarTitle = backStackEntry.arguments?.getString("title")
            val itemData = backStackEntry.arguments?.getParcelable<KnowledgeTreeItem>("itemData")

            if (topBarTitle != null && itemData != null) {
                KnowledgeScreen(
                    title = topBarTitle,
                    itemData = itemData,
                    navigateUp = { navigateUpWan(navController) },
                    navigateWebView = { title, link ->
                        toComposeWebView(navController, title, link)
                    },
                    updateMainScreenCollectListData = {
                        mainComposeViewModel.updateCollectArticleListData()
                    }
                )
            }
        }
        composable(RANK_ROUTE) {

            RankScreen {
                navigateUpWan(navController)
            }

        }
        composable(SCORE_ROUTE) {

            ScoreScreen {
                navigateUpWan(navController)
            }

        }
        composable(COLLECT_ROUTE) {


            CollectScreen(
                mainComposeViewModel,
                navigateUp = {
                    navigateUpWan(navController)
                },
                navigateWebView = { title, link ->
                    toComposeWebView(navController, title, link)
                })

        }
        composable(SEARCH_ROUTE) {

            SearchScreen(
                navigateUp = { navigateUpWan(navController) },
                navigateWebView = { title, link ->
                    toComposeWebView(navController, title, link)
                },
                updateMainScreenCollectListData = {
                    mainComposeViewModel.updateCollectArticleListData()
                }
            )

        }

    }
}






